<template>
  <el-dialog :visible.sync="modal" title="试卷详情" width="85%" custom-class="custom-dialog-test-add-topic-style"
    style="margin-top: -5vh;">
    <div class="title">
      <span>基本信息</span>
    </div>
    <div class="cell">
      <div class="item">
        <p class="key">
          <span>*</span>
          <span>试卷名称</span>
        </p>
        <p class="val">
          <span>{{ data.exam_name }}</span>
        </p>
      </div>
      <div class="item">
        <p class="key">
          <span>*</span>
          <span>培训类型</span>
        </p>
        <p class="val">
          <span>{{ data.type_name }}</span>
        </p>
      </div>
    </div>
    <div class="title">
      <span>题库内容</span>
    </div>
    <el-button @click="add" type="primary">添加题库</el-button>
    <div class="input">
      <span>单选题：</span>
      <el-input disabled v-model="data.dan_num" placeholder="单选题数量" style="width: 180px; margin-right: 3px;" />
      <span>多选题：</span>
      <el-input disabled v-model="data.duo_num" placeholder="多选题数量" style="width: 180px; margin-right: 3px;" />
      <span>判断题：</span>
      <el-input disabled v-model="data.pan_num" placeholder="判断题数量" style="width: 180px; margin-right: 3px;" />
      <el-button @click="random" type="primary">随机题库</el-button>
    </div>

    <el-tabs v-model="activeName" @tab-click="handleClick" style="margin-bottom: 120px;">
      <el-tab-pane label="单选题" name="first">
        <div class="list">
          <div class="topic" v-for="(item, index) in dan_list" :key="index">
            <span class="topic_type">{{ item.type == 1 ? '单选题' : item.type == 2 ? '多选题' : '判断题' }}:{{item.id}}</span>
            <div class="topic_content">
              <p class="topic_title">{{ item.title + '(题目分值:' + data.dan_point + ')' }}</p>
              <div class="option">
                <p v-for="(item2, index2) in item.options2" :key="index2">{{ item2 }}</p>
              </div>
              <p class="answer">正确答案：{{ item.answer }}</p>
              <div class="btns">
                <el-button size="mini" v-if="index != 0" @click="prev(item, index)">上移</el-button>
                <el-button size="mini" v-if="index != dan_list.length - 1" @click="next(item, index)">下移</el-button>
                <!--<el-button size="mini">编辑</el-button>-->
                <el-button size="mini" @click="deleteItem(item, index)">删除</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="多选题" name="second">
        <div class="list">
          <div class="topic" v-for="(item, index) in duo_list" :key="index">
            <span class="topic_type">{{ item.type == 1 ? '单选题' : item.type == 2 ? '多选题' : '判断题' }}:{{item.id}}</span>
            <div class="topic_content">
              <p class="topic_title">{{ item.title + '(题目分值:' + data.duo_point + ')' }}</p>
              <div class="option">
                <p v-for="(item2, index2) in item.options2" :key="index2">{{ item2 }}</p>
              </div>
              <p class="answer">正确答案：{{ item.answer }}</p>
              <div class="btns">
                <el-button size="mini" v-if="index != 0" @click="prev(item, index)">上移</el-button>
                <el-button size="mini" v-if="index != duo_list.length - 1" @click="next(item, index)">下移</el-button>
                <!--<el-button size="mini">编辑</el-button>-->
                <el-button size="mini" @click="deleteItem(item, index)">删除</el-button>
              </div>
            </div>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="判断题" name="third">
        <div class="topic" v-for="(item, index) in pan_list" :key="index">
          <span class="topic_type">{{ item.type == 1 ? '单选题' : item.type == 2 ? '多选题' : '判断题' }}:{{item.id}}</span>
          <div class="topic_content">
            <p class="topic_title">{{ item.title + '(题目分值:' + data.pan_point + ')' }}</p>
            <div class="option">
              <p v-for="(item2, index2) in item.options2" :key="index2">{{ item2 }}</p>
            </div>
            <p class="answer">正确答案：{{ item.answer }}</p>
            <div class="btns">
              <el-button size="mini" v-if="index != 0" @click="prev(item, index)">上移</el-button>
              <el-button size="mini" v-if="index != pan_list.length - 1" @click="next(item, index)">下移</el-button>
              <!--<el-button size="mini">编辑</el-button>-->
              <el-button size="mini" @click="deleteItem(item, index)">删除</el-button>
            </div>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>

    <selectTopic ref="select" @set-type-ids="setTypeIds"/>

    <div class="footer">
      <el-button @click="back">关闭</el-button>
      <el-button type="primary" size="medium" @click="submitForm()">保存</el-button>
    </div>
  </el-dialog>
</template>

<script>
import request from '@/libs/request';
import selectTopic from './selecttopic.vue'

export default {
  name: 'testDetails',
  filters: {},
  components: {
    selectTopic
  },
  data() {
    return {
      queryData: {},
      activeName: 'first',
      type: 1,
      data: {
        id: '',
        exam_name: '',
        type_name: '',
        dan_point:'',
        duo_point: '',
        pan_point: '',
      },
      random_dan: 0,
      random_duo: 0,
      random_pan: 0,
      modal: false,
      total: 0,
      dan_list: [],
      duo_list: [],
      pan_list: [],
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  mounted() {},
  methods: {
    random() {
      let loading = this.$loading()
      request({
        url: '/test/randomtopic',
        method: 'post',
        data: {
          dan: this.random_dan,
          duo: this.random_duo,
          pan: this.random_pan,
        }
      }).then(res => {
        loading.close();
        this.dan_list = res.data.dan
        this.duo_list = res.data.duo
        this.pan_list = res.data.pan
      }).catch(err => {
        loading.close();
        this.$message.error(err.msg)
      })
    },
    submitForm(){
      let loading = this.$loading()
      request({
        url: '/test/savetopic',
        method: 'post',
        data: {
          id: this.data.id,
          dan_list: this.dan_list,
          duo_list: this.duo_list,
          pan_list: this.pan_list,
          dan_num: 0,
          duo_num: 0,
          pan_num: 0,
        }
      }).then(res => {
        loading.close();
        this.modal = false;
      }).catch(err => {
        loading.close();
        this.$message.error(err.msg)
      })
    },
    showModal(id = '') {
      this.modal = true;
      this.data.id = id;
      this.getInfo();
    },
    handleClick(tab, event) {
      this.type = parseInt(tab.index) + 1
    },
    getInfo() {
      request({
        url: '/test/info',
        method: 'post',
        data: { id: this.data.id },
      }).then((res) => {
        this.data = res.data.data;
        this.dan_list = res.data.dan_list;
        this.duo_list = res.data.duo_list;
        this.pan_list = res.data.pan_list;
        this.dan_num = res.data.dan_num;
        this.duo_num = res.data.duo_num;
        this.pan_num = res.data.pan_num;
      });
    },
    add() {
      this.$refs.select.showModal(this.type)
    },
    search(d) {
      if (typeof d == 'number') {
        this.queryData.page = d;
      } else {
        this.queryData.page = 1;
      }
      this.getList();
    },
    sizeSearch(d) {
      this.queryData.size = d;
      this.search();
    },
    getList() {
    },
    init() {
    },
    setTypeIds(ids) {
      let newIds = []
      let nowIds = []
      if (this.type == 1) {
        // 单选
        nowIds = this.dan_list.map(item => {
          return item.id;
        })
      } else if (this.type == 2) {
        // 多选
        nowIds = this.duo_list.map(item => {
          return item.id;
        })
      } else {
        // 判断
        nowIds = this.pan_list.map(item => {
          return item.id;
        })
      }
      newIds = ids.filter(x => !nowIds.includes(x))
      request({
        url: '/question/list',
        method: 'post',
        data: {ids: newIds}
      }).then(res => {
        if (res.data.list) {
          res.data.list.forEach(item => {
            if (this.type == 1) {
              // 单选
              this.dan_list.push(item)
            } else if (this.type == 2) {
              // 多选
              this.duo_list.push(item)
            } else {
              // 判断
              this.pan_list.push(item)
            }
          })
        }
      })
    },
    deleteItem(data, index) {
      if (data.type == 1) {
        let item = this.dan_list.splice(index, 1)
      } else if (data.type == 2) {
        let item = this.duo_list.splice(index, 1)
      } else if (data.type == 3) {
        let item = this.pan_list.splice(index, 1)
      }
    },
    prev(data, index) {
      if (data.type == 1) {
        let item = this.dan_list.splice(index, 1)
        this.dan_list.splice(index - 1, 0, item[0])
      } else if (data.type == 2) {
        let item = this.duo_list.splice(index, 1)
        this.duo_list.splice(index - 1, 0, item[0])
      } else if (data.type == 3) {
        let item = this.pan_list.splice(index, 1)
        this.pan_list.splice(index - 1, 0, item[0])
      }
    },
    next(data, index) {
      if (data.type == 1) {
        let item = this.dan_list.splice(index, 1)
        this.dan_list.splice(index + 1, 0, item[0])
      } else if (data.type == 2) {
        let item = this.duo_list.splice(index, 1)
        this.duo_list.splice(index + 1, 0, item[0])
      } else if (data.type == 3) {
        let item = this.pan_list.splice(index, 1)
        this.pan_list.splice(index + 1, 0, item[0])
      }
    },
    back() {
      this.modal = false
    },
  },
};
</script>

<style scoped>
.title {
  width: 100%;
  display: flex;
  align-items: center;
  margin-bottom: 15px;
}

.title > span {
  font-size: 14px;
  color: #000;
  font-weight: bold;
}

.cell {
  width: 100%;
  display: flex;
  align-items: flex-start;
  margin-bottom: 20px;
}

.item {
  width: 50%;
  display: flex;
  align-items: center;
}

.key {
  font-size: 12px;
  flex-shrink: 0;
  margin-right: 10px;
  color: #7a7a7a;
}

.key > span:first-child {
  color: red;
  margin-right: 4px;
}

.val {
  color: #000;
  font-size: 12px;
  display: flex;
  align-items: center;
}

.val > span {
  margin-right: 10px;
}

.input > span {
  font-size: 12px;
  flex-shrink: 0;
}

.list {
  width: 100%;
  overflow: hidden;
}

.topic {
  width: 100%;
  padding: 15px;
  background: #f5f5f5;
  display: flex;
  align-items: flex-start;
  overflow: hidden;
  margin-bottom: 20px;
}

.topic_type {
  flex-shrink: 0;
  margin-right: 10px;
  padding: 6px 10px;
  background: #e8f4ff;
  line-height: 14px;
  color: #0256ff;
  border: 1px solid #84adff;
  border-radius: 2px;
}

.topic_content {
  width: 100%;
  overflow: hidden;
}

.topic_title {
  width: 100%;
  color: #000;
  font-size: 14px;
  margin-bottom: 10px;
}

.option {
  width: 100%;
  display: flex;
  align-items: flex-start;
}

.option p {
  width: 100%;
  margin-right: 30px;
  color: #000;
  font-size: 14px;
}

.option p:last-child {
  margin-right: 0;
}

.answer {
  width: 100%;
  margin-top: 20px;
  color: #000;
  font-size: 14px;
}

.btns {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  margin-top: 20px;
}

#editorContainer /deep/ .w-e-text-container {
  height: 150px !important;
}

.footer {
  width: 100%;
  height: 60px;
  position: absolute;
  bottom: 0;
  left: 0;
  z-index: 9;
  background: #ffffff;
  display: flex;
  align-items: center;
  justify-content: center;
  border-top: 1px solid #e6e6e6;
}
</style>
<style>
.custom-dialog-test-add-topic-style>.el-dialog__body {
  height: 80vh;
}
</style>